#include "DataStream.h"

#include <QDebug>
#include <QThread>
#include <QSerialPortInfo>

QStringList GetPortName()
{
    QStringList list;
    foreach(const QSerialPortInfo &info,QSerialPortInfo::availablePorts())
        list.append(info.portName());
    return list;
}

Serial::Serial(QString SerialPort, QSerialPort::BaudRate Baud, QSerialPort::Parity Parity, QSerialPort::StopBits StopBit, QSerialPort::DataBits DataBit)
{
    serial = new QSerialPort();
    serial->setPortName(SerialPort);
    serial->setBaudRate(Baud);
    serial->setParity(Parity);
    serial->setStopBits(StopBit);
    serial->setDataBits(DataBit);

    Type = Serial_Type;
}

Serial::~Serial()
{
    serial->close();
    delete serial;
}

void Serial::Open()
{
    if(!serial->open(QIODevice::ReadWrite))
    {
        qDebug()<<"Serial Open Erorr";
        return;
    }
    else
    {
        qDebug()<<"Serial Open";
    }

    connect(serial,SIGNAL(readyRead()),this,SLOT(ReceiveData()));
}

void Serial::Close()
{
    serial->close();
}

void Serial::ReceiveData()
{
    QByteArray info = serial->readAll();
    emit Data(info);
}

UDP::UDP(quint16 port)
{
    receiver = new QUdpSocket(this);
    receiver->bind(port,QUdpSocket::ShareAddress);
    connect(receiver,&QUdpSocket::readyRead,this,&UDP::ReceiveData);

    Type = NetWorkUDP_Type;
}

UDP::~UDP()
{
    receiver->close();
    delete receiver;
}

void UDP::Open()
{
    receiver->open(QIODevice::ReadWrite);
}

void UDP::Close()
{
    receiver->close();
}

void UDP::ReceiveData()
{
    while(receiver->hasPendingDatagrams()){
            QByteArray dataGram;
            //读取的数据报大小
            dataGram.resize(receiver->pendingDatagramSize());
            receiver->readDatagram(dataGram.data(),dataGram.size());
            emit Data(dataGram);
        }
}

TCP::TCP(QString host,quint16 port)
{
    tcpSocket = new QTcpSocket(this);
    //socket有数据来了，做处理
    connect(tcpSocket,&QTcpSocket::readyRead,
            this,&TCP::ReceiveData);
    connect(tcpSocket,SIGNAL(QAbstractSocket::SocketError),
            this,SLOT(onDisplayError(QAbstractSocket::SocketError)));

    m_hostName = host;
    m_port = port;

    Type = NetWorkTCP_Type;
}

TCP::~TCP()
{
    tcpSocket->close();
    delete tcpSocket;
}

void TCP::Open()
{
    tcpSocket->abort();
    tcpSocket->connectToHost(m_hostName,m_port);
}

void TCP::Close()
{
    tcpSocket->close();
}

void TCP::ReceiveData()
{
    QByteArray bt;
    bt.resize(tcpSocket->bytesAvailable());
    tcpSocket->read(bt.data(),bt.size());
    emit Data(bt);
}

DataStream::DataStream()
{

}

DataStream::~DataStream()
{
    foreach(Correspondence * cc ,m_Correspondence_List)
    {
        switch (cc->Type) {
        case NetWorkUDP_Type:{
            UDP * udp = (UDP *)cc;
            delete udp;
            qDebug()<<"delete udp";
            break;
        }
        case Serial_Type:{
            Serial * serial = (Serial *)cc;
            delete serial;
            qDebug()<<"delete serial";
            break;
        }
        case NetWorkTCP_Type:{
            TCP * tcp = (TCP *)cc;
            delete tcp;
            qDebug()<<"delete tcp";
            break;
        }
        }
    }
    m_Correspondence_List.clear();
}

void DataStream::CreateTcp(QString host, quint16 port)
{
    TCP * tcp = new TCP(host,port);
    tcp->Open();
    m_Correspondence_List.push_back(tcp);
    connect(tcp,SIGNAL(Data(QByteArray)),this,SLOT(DataProcessing(QByteArray)));
}

void DataStream::CreateUdp(quint16 port)
{
    UDP * udp = new UDP(port);
    udp->Open();
    m_Correspondence_List.push_back(udp);
    connect(udp,SIGNAL(Data(QByteArray)),this,SLOT(DataProcessing(QByteArray)));
}

void DataStream::CreateSerial(QString SerialPort, QSerialPort::BaudRate Baud, QSerialPort::Parity Parity, QSerialPort::StopBits StopBit, QSerialPort::DataBits DataBit)
{
    Serial * serial = new Serial(SerialPort,Baud,Parity,StopBit,DataBit);
    serial->Open();
    m_Correspondence_List.push_back(serial);
    connect(serial,SIGNAL(Data(QByteArray)),this,SLOT(DataProcessing(QByteArray)));
}

int DataStream::Size()
{
    return m_Correspondence_List.size();
}

void DataStream::DataProcessing(QByteArray data)
{
    qDebug()<<QString(data);
}
